package com.jt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findAll")
    @ResponseBody
    public List findAll(Model model){
        List<User> userList=userService.findAll();
        model.addAttribute("userList", userList);
        return userList;

    }

    @RequestMapping("/insert/{name}/{age}/{sex}")
    public String insert(User user){
        userService.insert(user);
        return "redirect:/findAll";
    }
    @RequestMapping("/lan")
    @ResponseBody
    public String lan(){
        System.out.println("controller的方法");
        return "test";
    }
   @RequestMapping("/update")
    public String update( Integer id, String name){
        User user=new User();
        user.setName(name);
        QueryWrapper qw=new QueryWrapper();
        qw.eq("id", id);
        userService.update(user,qw);
        return "redirect:/findAll";

    }
    @RequestMapping("/userajax")
    public String userajax(){
        return  "userajax";
    }
    @RequestMapping("/insert1")
    public String insert1(User user){
        userService.insert(user);
        return "redirect:/findAll";
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:/findAll";
    }
}
